package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.mapper.EduTeacherMapper;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-16
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {


    @Autowired
    EduTeacherService eduTeacherService;

    /**
     * 条件查询讲师
     * @param page
     * @param teacherQuery
     * @return
     */
    @Override
    public Page pageTeacher(Page page,TeacherQuery teacherQuery) {
        LambdaQueryWrapper<EduTeacher> queryWrapper = new LambdaQueryWrapper<>();
//        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(teacherQuery.getName()!=null,EduTeacher::getName,teacherQuery.getName());
        queryWrapper.gt(teacherQuery.getBegin()!=null,EduTeacher::getGmtCreate,teacherQuery.getBegin());
        queryWrapper.lt(teacherQuery.getEnd()!=null,EduTeacher::getGmtCreate,teacherQuery.getEnd());
        queryWrapper.eq(teacherQuery.getLevel()!=null,EduTeacher::getLevel,teacherQuery.getLevel());;

        queryWrapper.orderByDesc(EduTeacher::getGmtCreate);
        eduTeacherService.page(page,queryWrapper);
        return page;

    }

    /**
     * Banner页面显示的讲师
     * 4条记录
     */
     public List<EduTeacher> indexTeacherInfo(){

         QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
         wrapper.orderByAsc("gmt_modified");//取出最早的四条记录
         //拼接sql
         wrapper.last("limit 4");
         List<EduTeacher> list = baseMapper.selectList(wrapper);
         return list;
     }

    /**
     * 前台讲师分页
     * @param page
     * @return
     */
    @Override
    public Map<String, Object> pageList(Page page) {

        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");
        //查询数据库,结果封装在wrapper中
        baseMapper.selectPage(page, wrapper);
        List<EduTeacher> records = page.getRecords();long current = page.getCurrent();
        long pages = page.getPages();
        long size = page.getSize();
        long total = page.getTotal();
        boolean hasNext = page. hasNext();
        boolean hasPrevious = page.hasPrevious();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put( "items" , records);
        map.put( "current", current);
        map.put( "pages ", pages);
        map.put( "size", size);
        map.put( "total", total);
        map.put( "hasNext" , hasNext);
        map.put( "hasPrevious", hasPrevious );
        return map;

    }

}
